package com.applink.sell_proof.fragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.applink.sell_proof.QuizDetail;
import com.applink.sell_proof.R;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.QuestionDBHelper;
import com.applink.sell_proof.model.Answer;
import com.applink.sell_proof.model.Question;

public class QuestionFragmentDisable extends QuestionFragment {
	private ViewGroup mLayout;

	public static QuestionFragmentDisable newInstance(long questionId) {
		QuestionFragmentDisable f = new QuestionFragmentDisable();
		Bundle args = new Bundle();
		args.putLong(Constant.QUESTION_ID, questionId);
		f.setArguments(args);

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(R.layout.question, null,
					false);
		}
		mLayout.findViewById(R.id.next_button).setOnClickListener(this);
		mLayout.findViewById(R.id.back_button).setOnClickListener(this);
		mLayout.findViewById(R.id.question_container).setOnTouchListener(this);
		if (((QuizDetail) getActivity()).isFirstPager()) {
			mLayout.findViewById(R.id.back_button)
					.setVisibility(View.INVISIBLE);
		}
		if (((QuizDetail) getActivity()).isLastPager()) {
			((TextView) mLayout.findViewById(R.id.next_button))
					.setVisibility(View.INVISIBLE);
		}

		long questionId = getArguments().getLong(Constant.QUESTION_ID);
		Question question = QuestionDBHelper.getQuestion(questionId,
				((QuizDetail) getActivity()).getQuiizId());
		if (question != null) {
			((TextView) mLayout.findViewById(R.id.question_content))
					.setText(question.getContent());
			for (Answer answer : question.getAnswers()) {
				if (answer.getType().equals(Answer.TEXT_ANSWER)) {
					buildTextAnswer(answer);
				} else if (answer.getType().equals(Answer.SINGLE_ANSWER)) {
					buildSingleAnswer(answer);
				} else if (answer.getType().equals(Answer.MULT_ANSWER)) {
					buildMultAnswer(answer);
				}
			}
		}
		return mLayout;
	}

	protected void buildTextAnswer(Answer textAnswer) {
		LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		EditText editText = new EditText(getActivity());
		editText.setEnabled(false);
		editText.setLayoutParams(layoutParam);
		editText.setTextColor(getResources().getColor(R.color.black));
		editText.setBackgroundResource(R.drawable.textfield_hoto_light);
		editText.setTag(textAnswer.getId());
		((ViewGroup) mLayout.findViewById(R.id.question_container))
				.addView(editText);
		if (textAnswer.getUserReply() != null) {
			editText.setText(textAnswer.getUserReply());
		}

		// build result
		if (textAnswer.getResult() != null) {
			EditText resultEdittext = new EditText(getActivity());
			resultEdittext.setEnabled(false);
			editText.setLayoutParams(layoutParam);
			resultEdittext.setTextColor(getResources().getColor(R.color.black));

			JSONObject jsonResult;
			try {
				jsonResult = new JSONObject(textAnswer.getResult());
				JSONArray arrayResult = jsonResult.getJSONArray("Correct");
				String result = arrayResult.getString(0);
				resultEdittext.setText(result);
				((ViewGroup) mLayout.findViewById(R.id.question_container))
						.addView(editText);
			} catch (JSONException e) {
				Log.e(getTag(), e.toString());
			}
		}
	}

	protected void buildSingleAnswer(Answer singleAnswer) {
		RadioGroup radioGroup = new RadioGroup(getActivity());
		radioGroup.setTag(singleAnswer.getId());
		LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		try {
			JSONObject jsonOption = new JSONObject(singleAnswer.getOptions());
			JSONArray arrayOption = jsonOption.getJSONArray("Option");

			String result = null;
			if (singleAnswer.getResult() != null) {
				Log.e("question ",
						"correct answer : " + singleAnswer.getResult());
				JSONObject jsonResult = new JSONObject(singleAnswer.getResult());
				JSONArray arrayResult = jsonResult.getJSONArray("Correct");
				result = arrayResult.getString(0);
			}

			for (int i = 0; i < arrayOption.length(); i++) {
				String option = arrayOption.getString(i);
				RadioButton radioOption = new RadioButton(getActivity());
				radioOption.setEnabled(false);
				radioOption.setOnTouchListener(this);
				radioOption.setButtonDrawable(R.drawable.btn_radio_holo);
				Drawable icon = getResources().getDrawable(
						R.drawable.green_check);
				radioOption.setText(option);
				radioOption.setTextColor(getResources().getColor(R.color.blue));
				RadioGroup.LayoutParams radioGroupLayoutParam = new RadioGroup.LayoutParams(
						RadioGroup.LayoutParams.WRAP_CONTENT,
						RadioGroup.LayoutParams.WRAP_CONTENT);
				radioOption.setLayoutParams(radioGroupLayoutParam);
				radioGroup.addView(radioOption);
				Log.e("QuestionDisable", "index " + i + " user reply "
						+ singleAnswer.getUserReply() + " option " + option
						+ " correct " + result);
				if (singleAnswer.getUserReply().equals(option))
					radioOption.setChecked(true);
				if (result != null && result.equals(option)) {
					radioOption.setCompoundDrawablesWithIntrinsicBounds(null,
							null, icon, null);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		radioGroup.setLayoutParams(layoutParam);
		((ViewGroup) mLayout.findViewById(R.id.question_container))
				.addView(radioGroup);
	}

	protected void buildMultAnswer(Answer muiltAnswer) {
		LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		try {
			JSONObject jsonOption = new JSONObject(muiltAnswer.getOptions());
			JSONArray arrayOption = jsonOption.getJSONArray("Option");
			for (int i = 0; i < arrayOption.length(); i++) {
				String option = arrayOption.getString(i);
				CheckBox checkOption = new CheckBox(getActivity());
				checkOption.setOnTouchListener(this);
				checkOption.setEnabled(false);
				checkOption.setText(option);
				checkOption.setChecked(muiltAnswer.getUserReply().contains(
						option));
				checkOption.setTag(muiltAnswer);
				checkOption.setTextColor(getResources().getColor(R.color.blue));
				checkOption.setLayoutParams(layoutParam);
				((ViewGroup) mLayout.findViewById(R.id.question_container))
						.addView(checkOption);

				// result
				if (muiltAnswer.getResult() != null) {
					JSONObject jsonResult = new JSONObject(
							muiltAnswer.getResult());
					JSONArray arrayResult = jsonResult.getJSONArray("Correct");
					for (int j = 0; j < arrayResult.length(); j++) {
						if (arrayResult.getString(j).equals(option)) {
							Drawable icon = getResources().getDrawable(
									R.drawable.green_check);
							checkOption
									.setCompoundDrawablesWithIntrinsicBounds(
											null, null, icon, null);
							break;
						}
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			((QuizDetail) getActivity()).backQuestion();
			break;
		case R.id.next_button:
			if (((QuizDetail) getActivity()).isLastPager() == false) {
				((QuizDetail) getActivity()).nextQuestion();
			}
			break;
		default:
			break;
		}

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}
}
